package com.dspsemi.wodi;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.wheel.CityDBHelper;
import android.wheel.CityItem;
import android.wheel.OnWheelScrollListener;
import android.wheel.WheelView;
import android.wheel.adapter.ArrayWheelAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class InitInfoActivity extends Activity {

	private Button submit;
	private EditText nickname_et;
	private CityItem[] provinces;
	private WheelView provinceWheel;
	private WheelView cityWheel;

	private RadioButton boy;
	private RadioButton girl;

	private ProgressDialog pd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_initinfo);

		initViews();
		setListeners();
		initData();

	}

	private void initData() {
		CityDBHelper.getInstance().prepare(this);
		CityDBHelper.getInstance().open();

		provinces = CityDBHelper.getInstance().getAllProvicesArray();
		ArrayWheelAdapter<CityItem> adapter = new ArrayWheelAdapter<CityItem>(
				this, provinces);
		provinceWheel.setViewAdapter(adapter);
		provinceWheel.setCyclic(true);

		CityItem currentProvince = provinces[0];
		CityItem[] cities = CityDBHelper.getInstance()
				.getCitesArrayByProvinceID(currentProvince.getId());
		updateCities(cities);
	}

	private void updateCities(CityItem cities[]) {
		ArrayWheelAdapter<CityItem> adapter = new ArrayWheelAdapter<CityItem>(
				this, cities);
		adapter.setTextSize(25);
		cityWheel.setCyclic(true);
		cityWheel.setViewAdapter(adapter);
		cityWheel.setCurrentItem(0);
	}

	private void setListeners() {

		provinceWheel.addScrollingListener(new OnWheelScrollListener() {

			public void onScrollingStarted(WheelView wheel) {
			}

			public void onScrollingFinished(WheelView wheel) {
				int provincePosition = wheel.getCurrentItem();
				CityItem currentProvince = provinces[provincePosition];
				CityItem[] cities = CityDBHelper.getInstance()
						.getCitesArrayByProvinceID(currentProvince.getId());
				updateCities(cities);
			}
		});

		submit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				String nickname = nickname_et.getText().toString();
				if (nickname == null || nickname.equals("")) {
					toast("请输入昵称");
					return;
				}

				int sex = 1;
				if (boy.isChecked()) {
					sex = 1;
				} else {
					sex = 0;
				}
				submit(nickname, sex);

			}
		});
	}

	protected void submit(String nickname, int sex) {
		pd.show();
		new Thread() {
			public void run() {
				mHandler.sendEmptyMessage(1);
			};
		}.start();
	}

	private void initViews() {

		provinceWheel = (WheelView) findViewById(R.id.province);
		cityWheel = (WheelView) findViewById(R.id.city);
		submit = (Button) findViewById(R.id.submit);
		nickname_et = (EditText) findViewById(R.id.nickname_et);

		boy = (RadioButton) findViewById(R.id.boy);
		girl = (RadioButton) findViewById(R.id.girl);

		pd = new ProgressDialog(this);
		pd.setMessage("提交中");
		pd.setCancelable(false);
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			pd.dismiss();

			// 提交成功
			if (msg.what == 1) {

				String nickname = nickname_et.getText().toString();
				SharedPreferences sp = getSharedPreferences(nickname, 0);

				sp.edit().putString("nickname", "test").commit();

				Intent i = new Intent();
				i.setClass(InitInfoActivity.this, HallActivity.class);
				startActivity(i);
				finish();
				return;
			}
			// 提交失败
			if (msg.what == 0) {
				toast("提交失败，请稍后再试");
				return;
			}
		};
	};

	private void toast(String text) {
		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
	}
}
